package pro.shushi.pamirs.ce.hr.core.hook;

import org.springframework.stereotype.Component;
import pro.shushi.pamirs.framework.connectors.data.sql.Pops;
import pro.shushi.pamirs.meta.annotation.Hook;
import pro.shushi.pamirs.meta.api.core.faas.HookBefore;
import pro.shushi.pamirs.meta.api.dto.fun.Function;
import pro.shushi.pamirs.meta.api.dto.wrapper.IWrapper;
import pro.shushi.pamirs.ce.hr.api.HrSimpleCeModule;
import pro.shushi.pamirs.ce.hr.api.model.CustEmployee;

@Component
public class CustEmployeeHookBefore implements HookBefore {

    @Override
    @Hook(module = {HrSimpleCeModule.MODULE_MODULE}, model = {CustEmployee.MODEL_MODEL}, fun = {"create"})
    public Object run(Function function, Object... args) {
        if (args != null && args[0] != null) {
            CustEmployee arg = (CustEmployee) args[0];
            String identificationId = arg.getIdCard();
            IWrapper<CustEmployee> qw = Pops.<CustEmployee>lambdaQuery()
                    .from(CustEmployee.MODEL_MODEL)
                    .eq(CustEmployee::getIdCard, identificationId);
            Long count = new CustEmployee().count();
            if (count > 0) {
                throw new IllegalArgumentException("身份证号已存在");
            }
        }
        return args;
    }
}
